﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace AccidentCards
{
    class VehicleCard
    {
        static public void CreateVehicleCard(ref Int64 id, Int64 accidentId )
        {
            while (!dbServices.TestDbAccess())
            {
                DialogResult mesRslt = MessageBox.Show("Соединение потеряно.\nЧтобы повторить попытку подключения нажмите 'Повторить' или 'Отмена' для отмены действия.", "Ошибка", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);
                if (mesRslt == DialogResult.Retry)
                {
                    Application.DoEvents();
                }
                else
                    if (mesRslt == DialogResult.Cancel)
                    {
                        return;
                    }
            }

            //using (SqlConnection connection = new SqlConnection(globalConnectionString))
            try
            {

                SqlCommand cmd = new SqlCommand("CreateVehicleCard", dbServices.SqlConn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@id", SqlDbType.BigInt, 4);
                cmd.Parameters["@id"].Value = accidentId;

                cmd.Connection.Open();
                Object o = cmd.ExecuteScalar();
                id = Convert.ToInt64(o);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Ошибка, при создании нового события. Код 1: " + ex.ToString());
                return;

            }
            catch
            {
                MessageBox.Show("Ошибка, при создании нового события. Код 2.");
                return;

            }
        }

        public static int UpdateCard(
                                            long id,
                                            String enginNumber,              
                                            String bodyNumber,                 
                                            String chassisNumber,
                                            int creationYear,
                                            int passangersAmmount,
                                            int trailerAmmount,                 
                                            String carIdNumber,                   
                                            String carRegistrationCardNumber,
                                            String vehicleNumber,

                                            int escape,
                                            int vehicleType,
                                            int color,
                                            int malfunctions,
                                            int factoryCode,
                                            int modelCode,
                                            int wheelAndDrive,
                                            int damagePlaces,
                                            int registrationRegion,
                                            int organizationType,
                                            int ownershipType,
                                            int organizationName,
                                            int haveLicence,
                                            bool inSearch )
        {
            dbServices.Connect();
            var sqlCmd = new SqlCommand("UpdateVehicleCard", dbServices.SqlConn);

            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@id", SqlDbType.BigInt, 4);	         
            sqlCmd.Parameters.Add("@enginNumber", SqlDbType.NVarChar, 50);              
            sqlCmd.Parameters.Add("@bodyNumber", SqlDbType.NVarChar, 50);                 
            sqlCmd.Parameters.Add("@chassisNumber", SqlDbType.NVarChar, 50);
            sqlCmd.Parameters.Add("@creationYear", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@passangersAmmount", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@trailerAmmount", SqlDbType.Int, 4);                 
            sqlCmd.Parameters.Add("@carIdNumber", SqlDbType.NVarChar, 50);                   
            sqlCmd.Parameters.Add("@carRegistrationCardNumber", SqlDbType.NVarChar, 50);                 
            sqlCmd.Parameters.Add("@vehicleNumber", SqlDbType.NVarChar, 1024);
            sqlCmd.Parameters.Add("@escape", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@vehicleType", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@color", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@malfunctions", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@factoryCode", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@modelCode", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@wheelAndDrive", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@damagePlaces", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@registrationRegion", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@organizationType", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@ownershipType", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@organizationName", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@haveLicence", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@inSearch", SqlDbType.Bit);

            sqlCmd.Parameters["@id"].Value = id;	         
            sqlCmd.Parameters["@enginNumber"].Value = enginNumber;              
            sqlCmd.Parameters["@bodyNumber"].Value = bodyNumber;                 
            sqlCmd.Parameters["@chassisNumber"].Value = chassisNumber;
            sqlCmd.Parameters["@creationYear"].Value = creationYear;
            sqlCmd.Parameters["@passangersAmmount"].Value = passangersAmmount;
            sqlCmd.Parameters["@trailerAmmount"].Value = trailerAmmount;                 
            sqlCmd.Parameters["@carIdNumber"].Value = carIdNumber;                   
            sqlCmd.Parameters["@carRegistrationCardNumber"].Value = carRegistrationCardNumber;                 
            sqlCmd.Parameters["@vehicleNumber"].Value = vehicleNumber;
            sqlCmd.Parameters["@escape"].Value = escape;
            sqlCmd.Parameters["@vehicleType"].Value = vehicleType;
            sqlCmd.Parameters["@color"].Value = color;
            sqlCmd.Parameters["@malfunctions"].Value = malfunctions;
            sqlCmd.Parameters["@factoryCode"].Value = factoryCode;
            sqlCmd.Parameters["@modelCode"].Value = modelCode;
            sqlCmd.Parameters["@wheelAndDrive"].Value = wheelAndDrive;
            sqlCmd.Parameters["@damagePlaces"].Value = damagePlaces;
            sqlCmd.Parameters["@registrationRegion"].Value = registrationRegion;
            sqlCmd.Parameters["@organizationType"].Value = organizationType;
            sqlCmd.Parameters["@ownershipType"].Value = ownershipType;
            sqlCmd.Parameters["@organizationName"].Value = organizationName;
            sqlCmd.Parameters["@haveLicence"].Value = haveLicence;
            sqlCmd.Parameters["@inSearch"].Value = inSearch;

            try
            {
                sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return -1;
            }
            finally
            {
                sqlCmd.Dispose();
                dbServices.Close();
            }
            return 0;
        }

        static public DataTable GetAllVehicle(Int64 id)
        {           

            DataTable rslt = new DataTable();

            dbServices.Connect();
            var sqlCmd = new SqlCommand("GetVehicleList", dbServices.SqlConn);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@id", SqlDbType.BigInt, 4);
            sqlCmd.Parameters["@id"].Value = id;

                try
                {
                    rslt.Load(sqlCmd.ExecuteReader());
                }
                catch
                {
                    return null;
                }
            return rslt;
        }

        public static DataTable GetInfoById(long vhId)
        {
            var result = new DataTable("VehicleCardInfo");
            dbServices.Connect();
            var sqlAdapter = new SqlDataAdapter("GetVehicleCardInfoByID", dbServices.SqlConn);
            sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlAdapter.SelectCommand.Parameters.Add("@VehicleCardID", SqlDbType.BigInt, 4);
            sqlAdapter.SelectCommand.Parameters["@VehicleCardID"].Value = vhId;
            try
            {
                sqlAdapter.Fill(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                sqlAdapter.Dispose();
                dbServices.Close();
            }
            return result;
        }
    }
}
